package org.zjt

import org.apache.spark.mllib.linalg.Vectors

import scala.util.Random

/**
  * Hello world!
  *
  */
object App extends App {

  /*for (i <- 0 until 25) {
    val x = Random.nextDouble() * 100 % 100
    val y = Random.nextDouble() * 100 % 100
    println(s"zhang$i\t$x\t$y")
  }


  for (i <- 25 until 50) {
    val x = Random.nextDouble() * 1000 % 1000
    val y = Random.nextDouble() * 1000 % 1000
    println(s"zhang$i\t$x\t$y")
  }


  for (i <- 50 until 75) {
    val x = Random.nextDouble() * 500 % 500
    val y = Random.nextDouble() * 500 % 500
    println(s"zhang$i\t$x\t$y")
  }

  for (i <- 75 until 101) {
    val x = Random.nextDouble() * 800 % 800
    val y = Random.nextDouble() * 800 % 800
    println(s"zhang$i\t$x\t$y")
  }*/


  val array = Array(1, 2, 3, 2, 3, 4, 2)

  // 将tuple键和值拆成两个数组,  unzip: [(1,0),(2,1)]  -> (1,2),(0,1)
  println(array.tail.zipWithIndex.unzip) //除了第一个元素以外的[1:]
  println(array.head) //第一个元素

  println(array.tail(4)) //倒数第四个
  println(array.take(3).mkString(",")) //左边拿出三个
  println(array.takeRight(3).mkString(",")) //右边拿出三个

  println(array.drop(1).mkString(",")) //删除第一个
  println(array.zipWithIndex.mkString("\n")) //为元素加上索引（value，索引）

  println(array.last) //得到最后一个


  //稀疏向量：(特征个数,(index数组),(value数组))    描述非0变量的索引和值
  println(Vectors.sparse(20, array, Array(0.0, 2, 3, 2, 3, 4, 2))) //(20,[1,2,3,2,3,4,2],[1.0,2.0,3.0,2.0,3.0,4.0,2.0])
  //稠密向量：必须描述每一个变量的值
  println(Vectors.dense(Array(0.0, 2, 3, 2, 3, 4, 2)))

  println(args.mkString(","))

  println(Range(0, 5)) //[0,5) 前包后不包


  var sort = Array(0.0, 2, 3, 2, 3, 4, 2)
  sort = sort.sorted
  println(sort.mkString(","))

  for (i <- 0 until sort.length; j <- i + 1 until sort.length if j != i) {
    println(sort(i) + "   " + sort(j))
  }


  for (i <- 0 until 100) {
    print("key%s\t%s\n".format((math.random * 10).toInt, math.random * 100))
  }




  // split拆分成数组，并返回多个值
  "1,1,5.0".split(',') match {
    case Array(user, item, rate) => println(s"user:$user\titem:$item\trate:$rate")
  }


}
